package com.dmwork.service.interaction.controller.front.me;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dmwork.common.base.result.R;
import com.dmwork.common.base.util.JwtInfo;
import com.dmwork.common.base.util.JwtUtils;
import com.dmwork.service.interaction.entity.WorkCollect;
import com.dmwork.service.interaction.entity.WorkCollectGroup;
import com.dmwork.service.interaction.service.WorkCollectGroupService;
import com.dmwork.service.interaction.service.WorkCollectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 李天
 * @since 2021-03-20
 */
@Slf4j
@Transactional
@Api(description = "收藏分组管理")
@RestController
@RequestMapping("/front/dm_work/interaction/collect_group/me")
public class FrontMeWorkCollectGroupController {

    @Autowired
    private WorkCollectGroupService workCollectGroupService;
    @Autowired
    private WorkCollectService workCollectService;

    @ApiOperation("添加收藏分组")
    @PostMapping("add_group")
    public R addGroup(
            HttpServletRequest request,
            @RequestBody WorkCollectGroup workCollectGroup) {

        JwtInfo jwtToken = JwtUtils.getCustomerInfoByJwtToken(request);
        workCollectGroup.setCustomerId(jwtToken.getId());
        workCollectGroupService.save(workCollectGroup);
        return R.ok().message("创建成功");
    }

    @ApiOperation("修改收藏分组")
    @PutMapping("modify_group")
    public R editGroupName(@RequestBody WorkCollectGroup workCollectGroup) {
        if (workCollectGroup.getId().equals(WorkCollectGroup.DEFAULT_GROUP) || workCollectGroup.getId().equals(WorkCollectGroup.FAVOURITE_GROUP)) {
            return R.error().message("系统默认分组，不可修改");
        }
        workCollectGroupService.updateById(workCollectGroup);
        return R.ok().message("修改成功");
    }

    @ApiOperation("获取收藏分组列表")
    @GetMapping("get_group_list")
    public R getGroupList(HttpServletRequest request) {
        JwtInfo jwtToken = JwtUtils.getCustomerInfoByJwtToken(request);
        QueryWrapper<WorkCollectGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_id", jwtToken.getId())
                .or()
                .eq("id", WorkCollectGroup.DEFAULT_GROUP)
                .or()
                .eq("id", WorkCollectGroup.FAVOURITE_GROUP);
        List<WorkCollectGroup> workCollectGroupList = workCollectGroupService.list(queryWrapper);
        return R.ok().data("items", workCollectGroupList);
    }

    @ApiOperation("删除收藏分组")
    @DeleteMapping("remove_group/{groupId}")
    public R removeGroup(
            HttpServletRequest request,
            @ApiParam(value = "收藏分组", required = true) @PathVariable("groupId") String groupId) {
        if (groupId.equals(WorkCollectGroup.DEFAULT_GROUP) || groupId.equals(WorkCollectGroup.FAVOURITE_GROUP)) {
            return R.error().message("系统默认分组，无法删除");
        }

        JwtInfo jwtToken = JwtUtils.getCustomerInfoByJwtToken(request);

        //对当前收藏分组内的所有作品都进行取消收藏
        WorkCollect workCollect = new WorkCollect();
        workCollect.setCustomerId(jwtToken.getId());
        workCollect.setCollectGroupId(groupId);
        workCollectService.removeBatchByCollectGroup(workCollect);
        //删除当前收藏分组
        workCollectGroupService.removeById(groupId);
        return R.ok().message("删除成功");
    }

}

